Skip to content

fix(hooks): detect grep/read shell commands in Codex exec_command#1431

Open
octo-patch wants to merge 1 commit intooraios:mainfrom
octo-patch:fix/1394-codex-exec-command-hook
Open

fix(hooks): detect grep/read shell commands in Codex exec_command#1431
octo-patch wants to merge 1 commit intooraios:mainfrom
octo-patch:fix/1394-codex-exec-command-hook

Conversation

@octo-patch
Copy link
Copy Markdown
Contributor

Fixes #1394

Problem

serena-hooks remind --client=codex never counted Codex grep/read workflows because the Codex client sends all shell work as exec_command (with the actual command under tool_input.cmd). The previous detection checked only the tool name string, so is_grep_tool() and is_read_file_tool() always returned False for Codex — leaving the remind hook entirely blind to Codex exploration patterns.

Solution

  • Added _GREP_SHELL_COMMANDS and _READ_SHELL_COMMANDS frozensets listing common grep-like (grep, rg, ag, ack, fgrep, egrep) and read-file-like (cat, head, tail, sed, less, more, bat) shell commands.
  • Added _get_exec_command_name() helper that extracts the basename of the first token from tool_input.cmd, handling both bare names (rg) and path-prefixed invocations (/usr/bin/grep).
  • Updated is_grep_tool() and is_read_file_tool() to use these when the client is HookClient.CODEX and the tool name is exec_command; all other client paths are unchanged.

Testing

Added TestCodexExecCommandDetection class with 13 tests covering:

  • Positive cases: rg, grep, path-prefixed grep, agis_grep_tool()
  • Positive cases: sed, cat, head, tailis_read_file_tool()
  • Negative cases: ls, echo → neither
  • Non-exec_command tools for Codex still fall through to substring matching
  • End-to-end: repeated exec_command + rg / sed calls trip the deny after threshold

All 13 new tests pass. The 2 pre-existing test failures (test_outputs_activation_message, test_activate_command) were already present on main due to an unrelated activation message text change.

 oraios#1394)

When the Codex client sends shell work via `exec_command`, the tool name is
always `exec_command` and the actual command is under `tool_input.cmd`.  The
previous `is_grep_tool` / `is_read_file_tool` checks inspected only the tool
name and therefore never fired for Codex grep/read workflows.

Add `_GREP_SHELL_COMMANDS` and `_READ_SHELL_COMMANDS` frozensets and a helper
`_get_exec_command_name()` that extracts the basename of the first token in
`cmd`.  Both detection methods now use these to classify `exec_command` calls
when the client is `HookClient.CODEX`, leaving all other client paths unchanged.

Co-Authored-By: Octopus <liyuan851277048@icloud.com>
@MischaPanch
Copy link
Copy Markdown
Contributor

Thanks for the PR. We don't need the new tests, the current logic is very simple. Pls remove them, format the code and add an entry to the changelog. Then this can be merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

serena-hooks remind --client=codex misses grep/read workflows inside exec_command

2 participants